﻿Imports System.Data.OleDb
Public Class editprofile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            DisplayData()
            SaveConfirmation.Visible = False
        End If
    End Sub
    Protected Sub DisplayData()
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
        Dim sql = "SELECT * FROM userprofiles WHERE TravellerName=@f1"
        Dim cmd = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        conn.Open()
        Dim profileDr = cmd.ExecuteReader()
        profileDr.Read()
        Dim newEmailAddress = ""
        Dim newDescription = ""
        If Not IsDBNull(profileDr("EmailAddress")) Then newEmailAddress = profileDr.Item("EmailAddress")
        If Not IsDBNull(profileDr("Description")) Then newDescription = profileDr.Item("Description")
        If Not IsDBNull(profileDr("AvatarURL")) Then ProfilePic.ImageUrl = profileDr.Item("AvatarURL")
        description.Text = newDescription
        email.Text = newEmailAddress
        conn.Close()

    End Sub
    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim sql
        Dim cmd
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
        If Not fu_avatar.HasFile Then
            sql = "UPDATE userprofiles SET EmailAddress=@f1, Description=@f2 WHERE TravellerName=@f3"
            cmd = New OleDbCommand(sql, conn)
            cmd.Parameters.AddWithValue("@f1", email.Text)
            cmd.Parameters.AddWithValue("@f2", description.Text)
            cmd.Parameters.AddWithValue("@f3", User.Identity.Name)
        Else
            sql = "UPDATE userprofiles SET EmailAddress=@f1, Description=@f2, AvatarURL=@f3 WHERE TravellerName=@f4"
            cmd = New OleDbCommand(sql, conn)
            cmd.Parameters.AddWithValue("@f1", email.Text)
            cmd.Parameters.AddWithValue("@f2", description.Text)
            cmd.Parameters.AddWithValue("@f3", "UserProfileAvatar/" & User.Identity.Name & ".jpg")
            cmd.Parameters.AddWithValue("@f4", User.Identity.Name)
            Dim fileLocationOnServerHardDisk = Request.MapPath("UserProfileAvatar") & "/" & User.Identity.Name & ".jpg"
            fu_avatar.SaveAs(fileLocationOnServerHardDisk)
        End If
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        DisplayData()
        SaveConfirmation.Visible = True
    End Sub
End Class